<?php

namespace app\base\controller\api;

use app\base\service\ChanjingService;
use app\base\service\XjyService;
use think\admin\Controller;
use app\base\model\AiApiConfig;
use app\base\controller\api\Auth;
use app\base\model\AiVoice;
use app\base\service\MinimaxService;
use think\admin\extend\CodeExtend;


/**
 * 各平台api参数配置
 * Class Login
 * @package app\store\controller
 */
class ApiConfig extends Auth
{
    public function getList()
    {
        $list = AiApiConfig::mQuery()->where(['user_id'=>$this->user_id])->equal('type,cat')->order('id desc')->select()->toArray();
        $this->success('获取成功', $list);

    }

    public function saveData()
    {
        $data = $this->app->request->post();
        $data['user_id'] = $this->user_id;
        $id = AiApiConfig::mQuery()->where(['type'=>$data['type'],'user_id'=>$this->user_id])->value('id');
        switch ($data['type']){
            case 1:
                $data['cat'] = 1;
                break;
            case 2:
                $data['cat'] = 2;
                break;
            case 3:
                $data['cat'] = 3;
                break;
            case 4:
                $data['cat'] = 4;
                break;
            case 5:
                $data['cat'] = 1;
                break;
            case 6:
                $data['cat'] = 4;
                break;

        }
        if($id){
            AiApiConfig::mQuery()->where(['id'=>$id])->save($data);
        }else{
            AiApiConfig::mQuery()->save($data);
        }
        $this->success('保存成功');
    }
    public function getInfo()
    {
        $type= input('type');
        $data = AiApiConfig::mQuery()->where(['type'=>$type,'user_id'=>$this->user_id])->find();
        $this->success('获取成功', $data);
    }

    //音视频列表
    public function getVideoList()
    {

        $list = AiVoice::mQuery()->equal("platform_name,type")
            ->where(['user_id'=>$this->user_id,'is_deleted'=>0])->order('id desc')->select()->toArray();
        foreach ($list as &$item){
            $item['other_data'] = json_decode($item['other_data'],true);
           // $item['pic_url'] = '';
            //$item['audio_path'] = $item['file_url'];
            if(empty($item['pic_url'])){
                if($item['other_data'] && isset($item['other_data']['pic_url'])) {
                    $item['pic_url'] = $item['other_data']['pic_url'];
                }
                if($item['other_data'] && isset($item['other_data']['coverUrl'])) {
                    $item['pic_url'] = $item['other_data']['coverUrl'];
                }
            }

            if($item['other_data'] && isset($item['other_data']['audio_path']) && $item['id']>165) {
                $item['audio_path'] = $item['other_data']['audio_path'];
            }else{
                $item['audio_path'] = $item['file_url'];
            }
        }
        $this->success('获取成功', $list);
    }
    //保存音视频
    public function saveVideo()
    {
        $data = $this->app->request->post();
        $data['user_id'] = $this->user_id;
       // dump($data);
       // $data['platform_name'] = $this->app->db->name("ai_api_config")->where(['id'=>$data['api_config_id']])->value("name");
        $user = $this->app->db->name("ai_user")->where(['id'=>$this->user_id])->find();
        $nums = AiVoice::mQuery()->where(['user_id'=>$this->user_id,'type'=>$data['type'],'is_deleted'=>0])->count();

        if($data['type']==1){
            if($nums>=($user['js_voices']+$user['jl_voices'])){
                $this->error('模板数量已满');
            }
            if($data['platform_name']=="MINIMAX"){
                //上传文件到minimax
                // 解析 URL
                $parsedUrl = parse_url($data['file_url']);
                // 获取路径部分
                $file_path = $this->app->getRootPath()."public".$parsedUrl['path'];
                $r = MinimaxService::instance()->uploadFile($this->user_id,$file_path);
                if($r['code']==0){
                    $this->error($r['msg']);
                }
                //  dump($r);
                $data['file_id'] = $r['data'];
                //克隆声音
                $data['code'] =  CodeExtend::uniqidNumber(10, 'LY');
                $r = MinimaxService::instance()->voiceClone($this->user_id,$data['file_id'],$data['code']);
                if($r['code']==0){
                    $this->error($r['msg']);
                }
            }
            if($data['platform_name']=="蝉镜"){
               // dump($data['name']);dump($data['file_url']);
                $r = ChanjingService::instance()->createVoice($this->user_id,$data['name'],$data['file_url'],$data['model_type']);
                if($r['code']==0){
                    $this->error($r['msg']);
                }
                $data['code'] = $r['data'];
                $data['status'] = 2;
            }


        }


        if(empty($data['id'])){
            unset($data['id']);
            AiVoice::mQuery()->save($data);
        }else{
            $data['other_data'] = "";
            $data['is_reset'] =1;
            AiVoice::mQuery()->where(['id'=>$data['id']])->save($data);
        }
        $this->success('保存成功');
    }







    //删除音视频
    public function delVideo()
    {
        $id = input('id');
        $data = AiVoice::mQuery()->where(['id'=>$id,'user_id'=>$this->user_id])->find();
        if(empty($data)){
            $this->error('数据不存在');
        }
        if($data['type']==1){
            $c = $this->app->db->name("redian_task")->where(['voice'=>$id,'user_id'=>$this->user_id,'is_deleted'=>0])->count();
            if($c>0){
                $this->error('请先删除关联任务');
            }
        }else{
            $c = $this->app->db->name("redian_task")->where(['video'=>$id,'user_id'=>$this->user_id,'is_deleted'=>0])->count();
            if($c>0){
                $this->error('请先删除关联任务');
            }
        }
        $this->app->db->name("ai_voice")->where(['id'=>$id,'user_id'=>$this->user_id])->update(['is_deleted'=>1]);
        $this->success('删除成功');
    }
    //获取音视频详情
    public function getVideoInfo()
    {
        $id = input('id');
        $data = AiVoice::mQuery()->where(['id'=>$id,'user_id'=>$this->user_id])->find();
        $this->success('获取成功', $data);
    }

    //创建蝉镜数字人
    public function createAvatarChanjing()
    {
        $data = $this->app->request->post();
        $user = $this->app->db->name("ai_user")->where(['id'=>$this->user_id])->find();
        $nums = AiVoice::mQuery()->where(['user_id'=>$this->user_id,'type'=>$data['type'],'is_deleted'=>0])->count();
        if($nums>=($user['jl_videos']+$user['js_videos'])){
            $this->error('模板数量已满');
        }
        $data['user_id'] = $this->user_id;
        $r = ChanjingService::instance()->createAvatar($this->user_id,$data['name'],$data['file_url'],$data['train_type']);
        if($r['code']==0){
            $this->error($r['msg']);
        }
        $data['code'] = $r['data'];
        $data['status'] = 2;
        if(empty($data['id'])){
            unset($data['id']);
            AiVoice::mQuery()->save($data);
        }else{
            $data['is_reset'] =1;
            AiVoice::mQuery()->where(['id'=>$data['id']])->save($data);
        }
        $this->success('保存成功');
    }

    //创建小甲鱼数字人
    public function createAvatarXiaojiayu(){
        $data = $this->app->request->post();
        $user = $this->app->db->name("ai_user")->where(['id'=>$this->user_id])->find();
        $nums = AiVoice::mQuery()->where(['user_id'=>$this->user_id,'type'=>$data['type'],'is_deleted'=>0])->count();
        if($nums>=($user['jl_videos']+$user['js_videos'])){
            $this->error('模板数量已满');
        }
        $data['user_id'] = $this->user_id;
//        $r = XjyService::instance()->createAvatar($data['name'],$data['file_url']);
//        if($r['code']==0){
//            $this->error($r['msg']);
//        }
//        $data['code'] = $r['data'];
        $data['status'] = 2;
        $data['other_data'] = null;
        if(isset($data['pic_url'])){
            unset($data['pic_url']);
        }
        $id = 0;
        if(empty($data['id'])){
            unset($data['id']);
            $id = AiVoice::mQuery()->insertGetId($data);
        }else{
            $data['is_reset'] =1;
            $id = $data['id'];
            AiVoice::mQuery()->where(['id'=>$data['id']])->save($data);
        }
        sysqueue("{$data['name']}创建小甲鱼视频模板任务","xbase:CreateXjyVideoTemplate",1,['id'=>$id],$rscript = 1, $loops = 0);
        $this->success('保存成功');

    }

    //获取提示词列表
    public function getPromptList()
    {
        $type = input('type');
        $list = $this->app->db->name("ai_model_word")->where(['user_id'=>$this->user_id,'type'=>$type])->select();
        $this->success('获取成功', $list);
    }
    //保存提示词
    public function savePrompt(){
        $data = $this->app->request->post();
        $data['user_id'] = $this->user_id;
        $this->app->db->name("ai_model_word")->save($data);
        $this->success('保存成功');
    }
    //删除提示词
    public function delPrompt(){
        $id = input('id');
        $this->app->db->name("ai_model_word")->where(['id'=>$id,'user_id'=>$this->user_id])->delete();
        $this->success('删除成功');
    }

    //获取操作指南和客服二维码
    public function getGuide(){
        $this->success("suss",[
            'help_ewm'=>sysconf('help_ewm'),
            'help_link'=>sysconf('help_link')
        ]);
    }

    //失败重置小甲鱼视频模板生成
    public function agenCreateAvatarXiaojiayu()
    {
        $id = input('id');
        $data = AiVoice::mQuery()->where(['id'=>$id,'user_id'=>$this->user_id])->find();
        if(empty($data)){
            $this->error('数据不存在');
        }
        $this->app->db->name("ai_voice")->whereIn('id',$id)->update(['status'=>2]);

        sysqueue("{$data['name']}重新生成小甲鱼视频模板任务","xbase:CreateXjyVideoTemplate",1,['id'=>$id],$rscript = 1, $loops = 0);
        $this->success('操作成功');

    }

}
